{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bb008e6f",
   "metadata": {},
   "source": [
    "# BERT base model (cased)\n",
    "\n",
    "详细内容请看[Bert in PaddleNLP](https://github.com/PaddlePaddle/PaddleNLP/blob/develop/model_zoo/bert/README.md)。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "079266fb",
   "metadata": {},
   "source": [
    "Pretrained model on English language using a masked language modeling (MLM) objective. It was introduced in\n",
    "[this paper](https://arxiv.org/abs/1810.04805) and first released in\n",
    "[this repository](https://github.com/google-research/bert). This model is case-sensitive: it makes a difference between\n",
    "english and English.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c8220aa",
   "metadata": {},
   "source": [
    "Disclaimer: The team releasing BERT did not write a model card for this model so this model card has been written by\n",
    "the Hugging Face team.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8564477f",
   "metadata": {},
   "source": [
    "## Model description\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7365685d",
   "metadata": {},
   "source": [
    "BERT is a transformers model pretrained on a large corpus of English data in a self-supervised fashion. This means it\n",
    "was pretrained on the raw texts only, with no humans labelling them in any way (which is why it can use lots of\n",
    "publicly available data) with an automatic process to generate inputs and labels from those texts. More precisely, it\n",
    "was pretrained with two objectives:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c979d12",
   "metadata": {},
   "source": [
    "- Masked language modeling (MLM): taking a sentence, the model randomly masks 15% of the words in the input then run\n",
    "the entire masked sentence through the model and has to predict the masked words. This is different from traditional\n",
    "recurrent neural networks (RNNs) that usually see the words one after the other, or from autoregressive models like\n",
    "GPT which internally mask the future tokens. It allows the model to learn a bidirectional representation of the\n",
    "sentence.\n",
    "- Next sentence prediction (NSP): the models concatenates two masked sentences as inputs during pretraining. Sometimes\n",
    "they correspond to sentences that were next to each other in the original text, sometimes not. The model then has to\n",
    "predict if the two sentences were following each other or not.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdc00722",
   "metadata": {},
   "source": [
    "This way, the model learns an inner representation of the English language that can then be used to extract features\n",
    "useful for downstream tasks: if you have a dataset of labeled sentences for instance, you can train a standard\n",
    "classifier using the features produced by the BERT model as inputs.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9253a517",
   "metadata": {},
   "source": [
    "## How to use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fbfcd0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install --upgrade paddlenlp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6185db74",
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddle\n",
    "from paddlenlp.transformers import AutoModel\n",
    "\n",
    "model = AutoModel.from_pretrained(\"bert-base-cased\")\n",
    "input_ids = paddle.randint(100, 200, shape=[1, 20])\n",
    "print(model(input_ids))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e0ca3bd",
   "metadata": {},
   "source": [
    "```\n",
    "@article{DBLP:journals/corr/abs-1810-04805,\n",
    "author    = {Jacob Devlin and\n",
    "Ming{-}Wei Chang and\n",
    "Kenton Lee and\n",
    "Kristina Toutanova},\n",
    "title     = {{BERT:} Pre-training of Deep Bidirectional Transformers for Language\n",
    "Understanding},\n",
    "journal   = {CoRR},\n",
    "volume    = {abs/1810.04805},\n",
    "year      = {2018},\n",
    "url       = {http://arxiv.org/abs/1810.04805},\n",
    "archivePrefix = {arXiv},\n",
    "eprint    = {1810.04805},\n",
    "timestamp = {Tue, 30 Oct 2018 20:39:56 +0100},\n",
    "biburl    = {https://dblp.org/rec/journals/corr/abs-1810-04805.bib},\n",
    "bibsource = {dblp computer science bibliography, https://dblp.org}\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f14e9f06",
   "metadata": {},
   "source": [
    "<a href=\"https://huggingface.co/exbert/?model=bert-base-cased\">\n",
    "<img width=\"300px\" src=\"https://cdn-media.huggingface.co/exbert/button.png\">\n",
    "</a>\n",
    "\n",
    "> 此模型介绍及权重来源于 https://huggingface.co/bert-base-cased ，并转换为飞桨模型格式。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
